<?php

namespace app\admin\service;

use app\admin\model\BoxGame;
use app\admin\model\BoxPrivilegeTitle;
use think\Db;

class BoxPrivilegeService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxPrivilegeTitle();
    }

    public function getList()
    {
        $param = request()->param();
        // 查询条件
        $map = [];

        if (isset($param['game_id']) && !empty($param['game_id'])) {
            $map[] = ['game_id', '=', $param['game_id']];
        }

        if (!empty($param['time_range'][0]) && !empty($param['time_range'][1])) {
            $map[] = ['create_time', '>=', $param['time_range'][0]];
            $map[] = ['create_time', '<=', $param['time_range'][1]];
        }
        $data = $this->model->where($map)->order('id', 'desc')->page(PAGE, PERPAGE)->select();

        if (!empty($data)) {
            $gameData = BoxGame::field(['id', 'game_name'])->select()->toArray();
            $gameData = array_column($gameData, 'game_name', 'id');

            foreach ($data as &$val) {
                $val['game_name'] = $gameData[$val['game_id']] ?? '';
                $attribute        = json_decode($val['attribute'], true);
                $attributeData    = '';
                foreach ($attribute as $item) {
                    $attributeData .= $item['item'] . 'x' . $item['value'] . ';';
                }
                $val['attribute_data'] = $attributeData;
                $val['attribute']      = json_decode($val['attribute']);
                $val['status_time']    = $val['status_time'] == '0000-00-00 00:00:00' ? '' : $val['status_time'];
            }

        }
        //获取数据总数
        $count = $this->model->where($map)->count();

        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $data,
            "count" => $count,
        ];
    }

    public function edit()
    {
        $param = request()->param();
        if (empty($param['game_id'])) {
            return message("请选择游戏", false);
        }
        if (!empty($param['status'])) {
            $param['status_time'] = date("Y-m-d H:i:s");
        } else {
            $param['status_time'] = '0000-00-00 00:00:00';
        }

        if (isset($param['id']) && !empty($param['id'])) {
            $this->model->update($param, ['id' => $param['id']]);
        } else {
            $param['create_time'] = date('Y-m-d H:i:s');
            $this->model->save($param);
        }

        return message();
    }


    public function status()
    {
        $param = request()->param();
        if (!$param['id']) {
            return message('记录ID不能为空', false);
        }
        if (!isset($param['status'])) {
            return message('记录状态不能为空', false);
        }
        if (!empty($param['status'])) {
            $param['status_time'] = date("Y-m-d H:i:s");
        } else {
            $param['status_time'] = '';
        }
        $item = [
            'id'          => $param['id'],
            'status'      => $param['status'],
            'status_time' => $param['status_time']
        ];
        $this->model->edit($item);
        return message();
    }


    /**
     * 获取所有游戏列表
     * @return array
     */
    public function getGameList()
    {
        $gameList = BoxGame::field(['id', 'game_name'])->order("id DESC")->select();
        if (!empty($gameList)) {
            foreach ($gameList as &$item) {
                $item['game_name'] = $item['id'] . '/' . $item['game_name'];
            }
        }
        return message("操作成功", true, $gameList);
    }

}
